package com.chzc.fm.duotin.adapter.listview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.home.LastCommandDataList;
import com.chzc.fm.duotin.ui.home.utils.NumberTimeUtils;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/7/5.
 */
public class HomePlay1LastCommentListViewAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<LastCommandDataList> mCommendList;

    public HomePlay1LastCommentListViewAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public List<LastCommandDataList> getmCommendList() {

        return mCommendList;
    }

    public void setmCommendList(List<LastCommandDataList> mCommendList) {
        this.mCommendList = mCommendList;
    }

    @Override
    public int getCount() {
        return mCommendList ==null?0: mCommendList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCommendList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if(convertView==null){
            //判断是否缓存
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.home_play1_listview_comment,null);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
            holder.ivHeader = (ImageView) convertView.findViewById(R.id.iv_header);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();

        }
        //设置布局中控件要显示的视图
        //计算时间
        String createTimeString = mCommendList.get(position).getComment().getCreated();
        try {
            Date createdTie = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(createTimeString);

            long createdTimeMillions = createdTie.getTime();//将时间转换为毫秒值
            long nowTimeMillions = new Date().getTime();

            long time = createdTimeMillions - nowTimeMillions;
            String timeDistance = NumberTimeUtils.parseMillToXiaoShi(time);
            holder.tvTime.setText(timeDistance);

        } catch (ParseException e) {
            e.printStackTrace();
        }


        holder.tvTitle.setText(mCommendList.get(position).getUser().getReal_name());
        holder.tvContent.setText(mCommendList.get(position).getComment().getContent());

        ImageLoader.getInstance().displayImage(mCommendList.get(position).getUser().getImage_url(),holder.ivHeader, ImageUtil.getCircleOptions());

        return convertView;
    }

    private class ViewHolder{

        public TextView tvTitle,tvTime,tvContent;
        public ImageView ivHeader;
    }
}
